package com.costaccounting.base.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.costaccounting.base.domain.ZsCategory;
import com.costaccounting.base.domain.ZsItem;
import com.costaccounting.base.domain.ZsItemCategoryConfig;
import com.costaccounting.base.service.IZsCategoryService;
import com.costaccounting.base.service.IZsItemCategoryConfigService;
import com.costaccounting.base.service.IZsItemService;
import com.costaccounting.common.annotation.Log;
import com.costaccounting.common.core.controller.BaseController;
import com.costaccounting.common.core.domain.AjaxResult;
import com.costaccounting.common.core.domain.entity.SysDictData;
import com.costaccounting.common.core.page.TableDataInfo;
import com.costaccounting.common.enums.BusinessType;
import com.costaccounting.common.utils.poi.ExcelUtil;
import com.costaccounting.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目管理Controller
 *
 * @author H
 * @date 2021-11-06
 */
@RestController
@RequestMapping("/base/item")
public class ZsItemController extends BaseController {
    @Autowired
    private IZsItemService zsItemService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private IZsCategoryService categoryService;
    @Autowired
    private IZsItemCategoryConfigService itemCategoryConfigService;

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZsItem zsItem) {
        startPage();
        List<ZsItem> list = zsItemService.selectZsItemList(zsItem);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:item:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZsItem zsItem) {
        List<ZsItem> list = zsItemService.selectZsItemList(zsItem);
        ExcelUtil<ZsItem> util = new ExcelUtil<ZsItem>(ZsItem.class);
        return util.exportExcel(list, "项目管理数据");
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:item:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return zsItemService.getInfo(id);
        AjaxResult ajax = AjaxResult.success();
        ZsItem zsItem = zsItemService.selectZsItemById(id);
        SysDictData dictData = new SysDictData();
        dictData.setDictType("building_no");
        dictData.setStatus("0");
        List<SysDictData> buildingOptions = dictDataService.selectDictDataList(dictData);
        String[] buildIds = new String[0];
        String buildingIds = zsItem.getBuildingIds();
        if (null!=buildingIds&&!"".equals(buildingIds)) {
            buildIds=buildingIds.split(",");
        }

        ZsCategory category = new ZsCategory();
        category.setStatus("0");
        List<ZsCategory> zsCategories = categoryService.selectZsCategoryList(category);

        ZsItemCategoryConfig itemCategoryConfig=new ZsItemCategoryConfig();
        itemCategoryConfig.setItemId(zsItem.getId());
        List<ZsItemCategoryConfig> zsItemCategoryConfigs = itemCategoryConfigService.selectZsItemCategoryConfigList(itemCategoryConfig);
        zsItem.setBuildingListData(zsItemCategoryConfigs);

        ajax.put(AjaxResult.DATA_TAG, zsItem);
        JSONArray categoryIdList = new JSONArray();
        if (zsItem.getCategoryList() != null) {
            categoryIdList = JSONArray.parseArray(zsItem.getCategoryList());

        }
        ajax.put("buildIds", buildIds);
        ajax.put("categoryIdList", categoryIdList);
        ajax.put("categoryOptions", zsCategories);
        ajax.put("buildingOptions", buildingOptions);
        return ajax;
    }

    @PostMapping("/getCateList")
    public AjaxResult getCateList(@RequestBody ZsItem zsItem) {
        return zsItemService.getInfo(zsItem);
    }


    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('base:item:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZsItem zsItem) {
        return toAjax(zsItemService.insertZsItem(zsItem));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('base:item:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZsItem zsItem) {
        return toAjax(zsItemService.updateZsItem(zsItem));
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('base:item:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(zsItemService.deleteZsItemByIds(ids));
    }
}
